package com.haogu.controller;

import com.haogu.entity.*;
import com.haogu.mapper.BusinessStatusMapper;
import com.haogu.service.impl.BusinessService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/business")
public class BusinessController {
    @Resource
    private BusinessService businessService;

    /**
     * 商机主页面
     * @return
     */
    @RequestMapping(value = "/toBusiness")
    public String toBusiness(){
        return "business/index_business";
    }

    /**
     * 商机页面
     * @return
     */
    @RequestMapping(value = "/toList")
    public String  toList(){
        return "business/list_business";
    }
    /**
     *
     * 分页展示
     */
    @ResponseBody
    @RequestMapping(value = "/businessList")
    public Map<String,Object> list(HttpSession session,Integer page, Integer rows,String searchBy,String searchText){
        Map<String,Object> map1 = new HashMap();
        Map<String,Object> map2 = new HashMap();
        Object myBusiness = session.getAttribute("myBusiness");
        Object subBusiness = session.getAttribute("subBusiness");
        Object businesses = session.getAttribute("businesses");
        Object recycleBusiness = session.getAttribute("recycleBusiness");
        //今日、周、月新建
         if (businesses != null){
            map1.put("result",businesses);
            map2.put("data",map1);
            session.removeAttribute("businesses");
            return map2;
        }
         //我的商机
         if (myBusiness != null){
             map1.put("result",myBusiness);
             map2.put("data",map1);
             session.removeAttribute("myBusiness");
             return map2;
         }
         //下属商机
         if (subBusiness != null){
            map1.put("result",subBusiness);
            map2.put("data",map1);
            session.removeAttribute("subBusiness");
            return map2;
        }
         //回收站
         if (recycleBusiness != null){
             map1.put("result",recycleBusiness);
             map2.put("data",map1);
             session.removeAttribute("recycleBusiness");
             return map2;
         }
            Map<String, Object> map = new HashMap();
            map.put("offset", (page - 1) * rows);
            map.put("pageSize", rows);
            if ("bname".equals(searchBy)) {
                map.put("bname", searchText);
            }
            if ("names".equals(searchBy)) {
                map.put("names", searchText);
            }
            if ("origin".equals(searchBy)) {
                map.put("origin", searchText);
            }
            if ("nextstep".equals(searchBy)) {
                map.put("nextstep", searchText);
            }
            List<VBusiness> userBusinesses = businessService.listPage(map);
            int rows1 = businessService.rows();
            map1.put("totalRows", rows1);
            map1.put("result", userBusinesses);
            map2.put("data", map1);
            return map2;

    }

    /**
     * 添加页面
     * @return
     */
    @RequestMapping(value = "/toAddBusiness")
    public String toAdd(){
        return "/business/add_business";
    }

    /**
     * 负责人下拉框
     * @return
     */
    @RequestMapping(value = "/businessSystemUser")
    @ResponseBody
    public Map<String,Object> addUser(){
        Map<String,Object> map = new HashMap();
        List<User> users = businessService.selectUser();
        map.put("data",users);
        return map;
    }

    /**
     * 客户下拉框
     * @return
     */
    @RequestMapping(value = "/businessCustomer")
    @ResponseBody
    public Map<String,Object> addCustomer(){
        Map<String,Object> map = new HashMap<>();
        List<Customer> customers = businessService.selectCustomer();
        map.put("data",customers);

        return map;
    }

    /**
     *商机来源、商机类型下拉框
     * @return
     */
    @RequestMapping(value = "/businessType")
    @ResponseBody
    public Map<String,Object> addType(){
        Map<String,Object> map = new HashMap<>();
        List<VBusiness> businesses = businessService.listBusiness();
        map.put("data",businesses);
        return map;
    }

    /**
     * 联系人下拉框
     * @return
     */
    @RequestMapping(value = "/businessContacts")
    @ResponseBody
    public Map<String,Object> addContacts(){
        Map<String,Object> map = new HashMap<>();
        List<Contacts> contacts = businessService.selectContacts();
        map.put("data",contacts);
        return map;
    }

    /**
     * 商机状态下拉框
     * @return
     */
    @RequestMapping(value = "/businessStatus")
    @ResponseBody
    public Map<String,Object> addStatus(){
        Map<String,Object> map = new HashMap<>();
        List<BusinessStatusMapper> businessStatus = businessService.selectBusinessStatusMapper();
        map.put("data",businessStatus);
        return map;
    }

    /**
     * 保存商机添加
     * @return
     */
    @RequestMapping(value = "/BusinessAction")
    @ResponseBody
    public Map<String,Object> addBusiness(Business business ,HttpSession session){
        User user =(User) session.getAttribute("user");
        business.setCreatorUserId(user.getId());
        Map<String,Object> map = new HashMap<>();
        boolean b = businessService.addBusiness(business);
        map.put("success",b);
        return map;
    }

    /**
     * 查看商机
     * @param modelMap
     * @param id
     * @return
     */
    @RequestMapping(value = "/see")
    public String see(ModelMap modelMap, Integer id){
        Business business = businessService.selectBusinessById(id);
        modelMap.put("business",business);

        Customer customer = businessService.selectCustomerById(business.getCustomerId());
        modelMap.put("customer",customer);

        User user = businessService.selectUserByUserId(business.getOwnerUserId());
        modelMap.put("user",user);

        BusinessStatus businessStatus = businessService.selectStatusById(business.getStatusId());
        modelMap.put("businessStatus",businessStatus);

        Contacts contacts = businessService.selectContactsById(business.getContactsId());
        modelMap.put("contacts",contacts);
        return "business/see_business";
    }

    /**
     * 跳转到修改页面
     * @param modelMap
     * @param id
     * @return
     */
    @RequestMapping(value = "toEdit")
    public String toEdit(ModelMap modelMap,Integer id){
        Business business = businessService.selectBusinessById(id);
        modelMap.put("business",business);

        Customer customer = businessService.selectCustomerById(business.getCustomerId());
        modelMap.put("customer",customer);

        User user = businessService.selectUserByUserId(business.getOwnerUserId());
        modelMap.put("user",user);

        BusinessStatus businessStatus = businessService.selectStatusById(business.getStatusId());
        modelMap.put("businessStatus",businessStatus);

        Contacts contacts = businessService.selectContactsById(business.getContactsId());
        modelMap.put("contacts",contacts);
        return "business/edit_business";
    }

    /**
     * 修改
     * @param business
     * @return
     */
    @RequestMapping(value = "/edit")
    @ResponseBody
    public Map<String,Object> editBusiness(Business business){
        Map<String,Object> map = new HashMap<>();
        boolean b = businessService.editBusiness(business);
        map.put("success",b);
        return map;
    }

    /**
     * 逻辑删除
     * @param ids
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Map deleteRole(String ids){
        String[] split = ids.split(",");
        boolean flag = true;
        for(int i = 0; i < split.length;i++){
            int delete = businessService.deleteBusiness(Integer.parseInt(split[i]));
            if(delete == 0){
                flag = false;
            }
        }
        Map map = new HashMap();
        if(flag){
            map.put("success",true);
            map.put("message","删除成功");
        }else{
            map.put("success",false);
            map.put("message","删除失败");
        }
        return map;
    }

    /**
     * 今日、周、月新建
     * @param session
     * @param page
     * @param rows
     * @param type
     * @return
     */
    @RequestMapping(value = "/todayCreate")
    public String todayCreate(HttpSession session, @RequestParam(value = "page",defaultValue = "1") Integer page,
                              @RequestParam(value = "rows",defaultValue = "10") Integer rows,@RequestParam("type") String type){
        Map<String,Object> map = new HashMap();
        map.put("offset",(page-1)*rows);
        map.put("pageSize",rows);
        map.put("type",type);
        List<VBusiness> businesses = businessService.selectDay(map);
        session.setAttribute("businesses",businesses);
        return "business/list_business";
    }

    /**
     * 我的商机
     * @param session
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping(value = "/myCustomer")
    public String myCustomer(HttpSession session, @RequestParam(value = "page",defaultValue = "1") Integer page,
       @RequestParam(value = "rows",defaultValue = "10") Integer rows){
        User user =(User)session.getAttribute("user");
        Map<String,Object> map = new HashMap();
        map.put("offset",(page-1)*rows);
        map.put("pageSize",rows);
        map.put("ownerUser",user.getUsername());
        List<VBusiness> myBusiness = businessService.selectMyBusiness(map);
        session.setAttribute("myBusiness",myBusiness);
        return "business/list_business";
    }

    /**
     * 下属商机
     * @param session
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping(value = "/subCustomer")
    public String subCustomer(HttpSession session, @RequestParam(value = "page",defaultValue = "1") Integer page,
                             @RequestParam(value = "rows",defaultValue = "10") Integer rows){
        User user =(User)session.getAttribute("user");
        Map<String,Object> map = new HashMap();
        map.put("offset",(page-1)*rows);
        map.put("pageSize",rows);
        map.put("sortNum",user.getSortNum());
        List<VBusiness> subBusiness = businessService.selectSubBusiness(map);
        session.setAttribute("subBusiness",subBusiness);
        return "business/list_business";
    }

    /**
     * 回收站
     * @param session
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping(value = "/recycle")
    public String recycle(HttpSession session, @RequestParam(value = "page",defaultValue = "1") Integer page,
                              @RequestParam(value = "rows",defaultValue = "10") Integer rows){
        Map<String,Object> map = new HashMap();
        map.put("offset",(page-1)*rows);
        map.put("pageSize",rows);
        List<VBusiness> recycleBusiness = businessService.selectRecycle(map);
        session.setAttribute("recycleBusiness",recycleBusiness);
        return "business/list_business";
    }

    /**
     * 跳转邮件页面
     * @param id
     * @param modelMap
     * @param session
     * @return
     */
    @RequestMapping(value = "emails")
    public String email(Integer id,ModelMap modelMap,HttpSession session){
        Business business = businessService.selectBusinessById(id);
        Customer customer = businessService.selectCustomerById(business.getCustomerId());
        String zipcode = customer.getZipcode();
        modelMap.put("zipcode",zipcode);
        User user = (User)session.getAttribute("user");
        String email = user.getEmail();
        modelMap.put("email",email);
        return "marketing/sendemail_marketing";
    }

}
